#include "PlayerBehaviour.h"

PlayerBehaviour::PlayerBehaviour(Player* player) : Behaviour(player), player(player) {
}

PlayerBehaviour::~PlayerBehaviour() {
}

void PlayerBehaviour::update(float step) {
    KeyBinder* keyBinder = KeyBinder::getInstance();

    if(keyBinder->isKeyPressed(KeyBinder::ACCELERATE)) {
        player->changeSpeed(-0.1f);
    } else if(keyBinder->isKeyPressed(KeyBinder::DECELERATE)) {
        player->changeSpeed(0.1f);
    }

    if(keyBinder->isKeyPressed(KeyBinder::FORWARD)) {
        player->rotate(100 * step, glm::vec3(1.0f, 0.0f, 0.0f));
    } else if(keyBinder->isKeyPressed(KeyBinder::BACKWARD)) {
        player->rotate(-100 * step, glm::vec3(1.0f, 0.0f, 0.0f));
    }

    if(keyBinder->isKeyPressed(KeyBinder::LEFT)) {
        player->rotate(100 * step, glm::vec3(0.0f, 1.0f, 0.0f));
    } else if(keyBinder->isKeyPressed(KeyBinder::RIGHT)) {
        player->rotate(-100 * step, glm::vec3(0.0f, 1.0f, 0.0f));
    }

    player->translate(glm::vec3(0.0f, 0.0f, -step * player->getSpeed()));

    if(keyBinder->isKeyTapped(KeyBinder::SHOOT)) {
        Rocket* rocket = new Rocket(parent->getLocation());
        rocket->setTransform(parent->getTransform());
        glm::vec3 speed(0.0f, 0.0f, 40.0f);

         if(player->getSpeed()<0)
        {
            float spd = -player->getSpeed();
            speed.z +=spd;
        }

        // Change the velocity of the bullet, so it fires in the direction the player
        // is looking.
        glm::mat4 trans = player->getTransform();
        float x = speed.x * trans[0][0] + speed.y * trans[1][0] + speed.z * trans[2][0];
        float y = speed.x * trans[0][1] + speed.y * trans[1][1] + speed.z * trans[2][1];
        float z = speed.x * trans[0][2] + speed.y * trans[1][2] + speed.z * trans[2][2];
        rocket->setVelocity(glm::vec3(x, y, z));

        Global::getWorld()->addLater.push_back(rocket);
    }
}
